package com.jrx.anytxn.transaction.job.dailyAccount.config;

import com.jrx.anytxn.param.entity.PrOrganizationTable;
import com.jrx.anytxn.param.service.system.IOrganizationTableService;
import com.jrx.anytxn.transaction.config.AnytxnBatchProperties;
import com.jrx.anytxn.transaction.constant.TransBizConstant;
import com.jrx.anytxn.transaction.entity.BtDailyAccountList;
import com.jrx.anytxn.transaction.entity.TlDisbursementLog;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountInitReader;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountInitListener;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountInitProcessor;
import com.jrx.anytxn.transaction.job.dailyAccount.step.DailyAccountInitWriter;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;
import org.springframework.transaction.PlatformTransactionManager;

import javax.annotation.Resource;
import java.util.Date;

/**
 * 日终T24核心汇总记账初始化
 *
 * @Author yhy
 * @Date 2023/4/28 15:10
 */
@Configuration
@EnableConfigurationProperties(AnytxnBatchProperties.class)
public class DailyAccountInitConfiguration {

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;

    @Autowired
    public StepBuilderFactory stepBuilderFactory;

    @Autowired
    private AnytxnBatchProperties anytxnBatchProperties;

    @Autowired
    private PlatformTransactionManager platformTransactionManager;

    @Autowired
    private IOrganizationTableService organizationTableService;

    @Autowired
    private DailyAccountInitListener dailyAccountInitListener;

    @Autowired
    private DailyAccountInitProcessor dailyAccountInitProcessor;

    @Autowired
    private DailyAccountInitWriter dailyAccountInitWriter;

    @Bean
    public Job dailyAccountInitJob(){
        return jobBuilderFactory.get("dailyAccountInitJob")
                // 清空批扣初始化列表
                .listener(dailyAccountInitListener)
                // 初始化兴安借产品放款记录
                .start(dailyAccountInitStep())
                .build();
    }

    @Bean
    public Step dailyAccountInitStep(){
        return stepBuilderFactory.get("dailyAccountInitStep")
                // 表示一批处理100条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .<TlDisbursementLog, BtDailyAccountList>chunk(anytxnBatchProperties.getDailyAccountInitConfiguration().getChunkLimit())
                // 获取日终T24核心汇总记账未记账放款记录
                .reader(dailyAccountInitReader())
                // 处理组装初始化数据
                .processor(dailyAccountInitProcessor)
                // 保存初始化数据
                .writer(dailyAccountInitWriter)
                .transactionManager(platformTransactionManager)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anytxnBatchProperties.getDailyAccountInitConfiguration().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<TlDisbursementLog> dailyAccountInitReader() {
        PrOrganizationTable org = organizationTableService.findByOrgIdForBatch(TransBizConstant.DEFAULT_ORG_ID);
        Date preBusinessDate = org.getToday();
        return new DailyAccountInitReader(preBusinessDate, sqlSessionFactory, anytxnBatchProperties.getDailyAccountInitConfiguration().getPageSize());
    }
}
